package com.bluefox.search.lucene.convertor;


import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexableField;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Component
public class MapLuceneConvertor implements LuceneConvert<Map>{
    @Override
    public Map docToObject(Document doc) {
        Map map = new HashMap();
        //  转换成map对象
        for (IndexableField field : doc.getFields()) {
            map.put(field.name(), field.stringValue());
        }
        return map;
    }

    @Override
    public Document objectToDoc(Map map) {
        Document doc = new Document();
        Iterator<Map.Entry> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = entries.next();
            doc.add(new TextField((String) entry.getKey() ,(String) entry.getValue() , Field.Store.YES));
        }
        return doc;
    }
}
